﻿using DataServer.Entities;
using IsPay.Logic;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay.View
{
    public partial class EditCust : System.Web.UI.Page
    {
        private String returnmsge = "";
        public UserProfile user = new UserProfile();
        public IEnumerable<UserProfile> lsuser = new List<UserProfile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null || Session["USERID"].ToString().Length <= 0)
            {
                Response.Write("<script>top.location='../Default.aspx'</script>");
            }
            if (Request.Params["ActionType"] != null)
            {
                String actionType = Request.Params["ActionType"].ToString();
                if (actionType.Equals("addsingle"))
                {
                    returnmsge = addUserSingle();
                    lblmsg.Text = returnmsge;
                }
                else if (actionType.Equals("query"))
                {
                    String strquserid = Request.Params["txtsusrid"].ToString().Trim();
                    String strqusername = Request.Params["txtsusrname"].ToString().Trim();
                    String strisactive = Request.Params["ISACTIVE"] != null ? Request.Params["ISACTIVE"].ToString().Trim() : "False";
                    UserSer user = new UserSer();
                    lsuser = user.QueryUser(strquserid, strqusername, strisactive, "CUST");


                }
                else if (actionType.Equals("modify"))
                {
                    String edittablejasn = Request.Params["updatejason"].ToString().Trim();
                    DataTable dtedit = JasonToDataTable(edittablejasn);
                    if (dtedit != null && dtedit.Rows.Count > 0)
                    {
                        try
                        {
                            List<UserProfile> lsuser = DataTableToUser(dtedit);
                            UserSer usedit = new UserSer();
                            String strresoult = usedit.ModifyUsers(lsuser, Session["USERID"].ToString());
                            if (strresoult.Equals("OK"))
                            {
                                lblmsg.Text = "更新成功";
                            }
                            else
                            {
                                lblmsg.Text = strresoult;
                            }

                        }
                        catch (Exception ex)
                        {
                            lblmsg.Text = ex.Message;
                        }
                    }



                }
            }
        }
        private List<UserProfile> DataTableToUser(DataTable dtuser)
        {
            List<UserProfile> lsuser = new List<UserProfile>();
            foreach (DataRow dr in dtuser.Rows)
            {
                UserProfile us = new UserProfile();
                us.UserId = dr["USERID"].ToString();
                us.UserName = dr["USERNAME"].ToString();
                us.Email = dr["EMAIL"].ToString();
                us.Telephone = dr["TELEPHONE"].ToString();
                us.AuthGroup = "AG02";
                us.UserType = "CUST";
                user.AccuntId = "";
                us.Pwd = dr["PWD"].ToString().Trim().Length > 0 ? CodeMange.getMD5StringCode(dr["PWD"].ToString().Trim()) : ""; 
                if (dr["deleteflag"].ToString().Equals("True"))
                {
                    us.IsActive = "0";
                }
                else
                {
                    us.IsActive = "1";
                }
                us.LastLoginTimeKey = "";
                us.LastPCIp = Request.UserHostAddress;
                us.LastPCName = Request.UserHostName;
                us.LastPCMac = "";
                lsuser.Add(us);
            }

            return lsuser;
        }
        private DataTable JasonToDataTable(String edittablejasn)
        {
            JArray ja = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(edittablejasn);
            int irow = 0;
            DataTable dt = new DataTable();
            foreach (JObject jobj in ja)
            {

                if (irow == 0)
                {
                    foreach (JProperty pry in jobj.Properties())
                    {
                        dt.Columns.Add(pry.Name.ToUpper());
                    }

                }
                DataRow drnew = dt.NewRow();
                foreach (JProperty pryv in jobj.Properties())
                {
                    drnew[pryv.Name.ToUpper()] = pryv.Value;
                }
                dt.Rows.Add(drnew);
                irow++;
            }
            return dt;
        }

        private String addUserSingle()
        {
            String strmsg = "添加完成";
            UserSer checkser = new UserSer();
            user.UserId = Request.Params["txtuserid"].ToString().Trim();
            user.UserName = Request.Params["txtusername"].ToString().Trim();
            user.Email = Request.Params["txtmail"].ToString().Trim();
            user.Telephone = Request.Params["txtphone"].ToString();

            if (user.UserId == null || user.UserId.Length <= 0)
            {

                lblmsg.Text = "请填写用户ID";
                return "请填写用户ID";
            }
            else
            {


            }

            if (user.UserName == null || user.UserName.Length <= 0)
            {

                lblmsg.Text = "请填写用户名";
                return "请填写用户名";

            }
            else
            {

            }

            if (Request.Params["txtusepwd1"] == null || Request.Params["txtusepwd1"].ToString().Trim().Length <= 0 || Request.Params["txtusepwd2"] == null || Request.Params["txtusepwd2"].ToString().Trim().Length <= 0)
            {
                lblmsg.Text = "请输入密码";
                return "请输入密码";
            }
            else if (!Request.Params["txtusepwd1"].ToString().Trim().Equals(Request.Params["txtusepwd2"].ToString().Trim()))
            {
                lblmsg.Text = "初始密码和确认密码不一致";
                return "初始密码和确认密码不一致";
            }
            else
            {
                //byte[] result = Encoding.Default.GetBytes(Request.Params["txtusepwd1"].ToString().Trim());
                //MD5 md5 = new MD5CryptoServiceProvider();
                //byte[] output = md5.ComputeHash(result);
                //String strMD5pwd = BitConverter.ToString(output).Replace("-", "");
                //user.Pwd = strMD5pwd;
                user.Pwd = CodeMange.getMD5StringCode(Request.Params["txtusepwd1"].ToString().Trim());
            }
            if (user.Email == null || user.Email.Length <= 0)
            {
                lblmsg.Text = "请输入邮箱";
                return "请输入邮箱"; ;
            }
            else
            {
                Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
                if (r.IsMatch(user.Email))
                {


                }
                else
                {
                    lblmsg.Text = "邮箱格式错误";
                    return "邮箱格式错误";
                }
            }

            if (user.Telephone == null || user.Telephone.Length <= 0)
            {
                lblmsg.Text = "请输入电话";
                return "请输入电话";
            }
            else
            {
                Regex r = new Regex("^\\d{11}$");
                if (r.IsMatch(user.Telephone))
                {


                }
                else
                {
                    lblmsg.Text = "电话格式错误";
                    return "电话格式错误";
                }
            }
            if (checkser.CheckUserDouble("UserId", user.UserId))
            {
                lblmsg.Text = "用户已经存在";
                return "用户已经存在"; ;
            }
            if (checkser.CheckUserDouble("UserName", user.UserName))
            {
                lblmsg.Text = "用户名已经存在";
                return "用户名已经存在";
            }
            if (checkser.CheckUserDouble("Email", user.Email))
            {
                lblmsg.Text = "邮箱已经存在";
                return "邮箱已经存在";
            }
            if (checkser.CheckUserDouble("Telephone", user.Telephone))
            {
                lblmsg.Text = "电话已经存在";
                return "电话已经存在";
            }

            user.LastLoginTimeKey = DateTime.Now.ToString("yyyyMMddHHmmss");
            user.IsActive = "1";
            user.LastPCIp = Request.UserHostAddress;
            user.LastPCMac = "";
            user.LastPCName = Request.UserHostName;
            user.AuthGroup = "AG02";
            user.UserType = "CUST";
            user.AccuntId = "";
            if (checkser.AddUser(user))
            {
                strmsg = "添加成功";
            }
            else
            {
                strmsg = "添加失败";
            }

            return strmsg;
        }
    }
}